<template>
  <el-dialog
    title="更改分发企业"
    width="460px"
    :visible.sync="showTask"
    :close-on-click-modal="false"
    @close="handleClose"
    :modal-append-to-body="false"
  >
    <el-form :model="info" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="分发企业">
        <el-select
          v-model="info.complaintCompanyId"
          filterable
          placeholder="请选择"
          @change="handle($event)"
          clearable
        >
          <el-option
            v-for="item in complaintCompanyOptions"
            :key="item.id"
            :label="item.complaintName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button class="cancel" @click="handleClose">取消</el-button>
      <el-button
        type="primary"
        class="save"
        :loading="industryLoading"
        @click="submit"
        v-sdc
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'dialogCompanyLzh',
  props: {
    showTaskProp: {
      type: Boolean,
      default: false,
    },
    childData: {
      type: Object,
      required: true,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      showTask: false,
      curIndex: '',
      info: {
        id: '',
        complaintCompanyId: '',
        complaintCompanyNames: '',
      },
      initInfo: {},
      complaintCompanyOptions: [],
      industryLoading: false,
    }
  },
  watch: {
    showTaskProp(val) {
      this.showTask = val
      this.value = []
      if (val) {
        this.getIndustry()
        this.info.complaintCompanyId = this.childData.complaintCompanyIds
          ? this.childData.complaintCompanyIds[0]
          : ''
      }
    },
  },
  mounted() {},
  methods: {
    getIndustry() {
      this.axios({
        method: 'post',
        url: 'api-r/ezrightbackpay/expand/getCompanyAccount',
        data: {
          uscc: this.childData.uscc,
        },
      })
        .then((res) => {
          this.complaintCompanyOptions = res.data.result
          const complaintCompanyId = this.childData.complaintCompanyIds[0]
          if (complaintCompanyId && res.data.result.length > 0) {
            this.complaintCompanyOptions.forEach((item, index) => {
              if (item.id == complaintCompanyId) {
                this.curIndex = index
              }
            })
          }
        })
        .catch((error) => {})
    },
    handle(id) {
      this.info.complaintCompanyId = id
      var item = this.complaintCompanyOptions.filter((item) => item.id == id)
      this.info.complaintCompanyNames = item[0].complaintName
    },
    submit() {
      if (this.info.complaintCompanyId) {
        this.handle(this.info.complaintCompanyId)
        this.industryLoading = true
        let params = {
          id: this.childData.id,
          complaintCompanyId:
            this.info.complaintCompanyId +
            ':' +
            this.info.complaintCompanyNames,
          uscc: this.childData.uscc,
        }
        this.axios
          .post('api-r/rightbackpay/expand/edit', params)
          .then((res) => {
            if (!res.data.hasError) {
              this.$message({
                type: 'success',
                message: res.data.msg,
              })
              this.industryLoading = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'warning',
                message: res.data.errorMsg,
              })
              this.industryLoading = false
            }
          })
      } else {
        this.$message({
          type: 'warning',
          message: '请选择分发企业',
        })
      }
    },
    handleClose(index) {
      if (this.showTask) {
        this.industryLoading = false
        this.showTask = false
      }
      this.$emit('callback', { success: index == 1 ? true : false })
    },
  },
}
</script>
<style lang="less" scoped></style>
